/**
 * 绘制路线
 * @param {Earth} options.earth 路线的渲染容器
 * @param {Array} options.positions 路线轮廓数据 [ lng, lat, lng, lat... ]
 * @param {Array} options.color 路线填充颜色 colorArray
 * @param {Number} options.width 路线宽度
 * @param {Boolean} options.loop 路线是否为环线 首尾相连
 * @param {Boolean} options.playing 路线是否播放
 * @param {Boolean} options.currentSpeed 路线播放速度
 * @param {Boolean} options.changeStatus 是否可编辑属性
 * @param {object} options.data 临时数据存放
 * 
 * @methods destroy 销毁
 * 
 * @returns {DrawPath} DrawPath实例  path = XE.Obj.Path实例   polyLine = XE.Obj.Polyline实例
 */

function DrawPath (options) {
    let radian = Math.PI / 180;
    let positions = options.positions.map(point => {
        return [
            point[0] * radian,
            point[1] * radian,
            point[2] || 0
        ]
    })

    let path = pathInit(options, positions)
    let polyLine = lineInit(options, positions)

    this.polyLine = polyLine
    this.path = path
    this.data = options.data || null
}

DrawPath.prototype.setPositions = function (options) {
    let radian = Math.PI / 180;
    let positions = options.map(point => {
        return [
            point[0] * radian,
            point[1] * radian,
            point[2] || 0
        ]
    })
    this.polyLine.positions = positions
    this.path.positions = positions
    this.path.slices = positions.length
}

DrawPath.prototype.destroy = function () {
    this.polyLine.destroy()
    this.path.destroy()
}

DrawPath.prototype.setShow = function (options) {
    this.polyLine.show = options
}

// 创建路径
function pathInit (options, positions) {
    let path = new XE.Obj.Path(options.earth);
    path.currentSpeed = options.currentSpeed || 20
    path.slices = positions.length
    path.loop = options.loop || false
    path.alwaysAlongThePath = true
    path.loopPlay = false
    path.playing = options.playing || false
    path.positions = positions
    path.show = false

    path.preUpdateEvalString = options.preUpdateEvalString || ''

    return path

}

function lineInit (options, positions) {
    let polyline = new XE.Obj.Polyline(options.earth);

    polyline.xbsjFromJSON({
        customProp: options.data,
        ground: options.ground === undefined ? true : options.ground,
        loop: options.loop || false,
        positions: positions || [],
        show: options.show === undefined ? true : options.show,
        width: options.width || 2
    })
    polyline.material.XbsjColorMaterial.color = options.color || [ 1,1,1,1 ]

    return polyline
}

export default DrawPath